<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

// import {getVirtulData, getPieSeries, getPieSeriesUpdate} from '@/utils/pieChart'

const pieRadius = 40
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    datas: {
      default: 123
    },
    range: {
      type: Array,
      default: () => {}
    }
  },
  data() {
    const cellSize = [100, 100]

    return {
      chart: null,
      cellSize: [100, 100]
    }
  },
  computed: {
    options() {
      return {
        tooltip: {
          axisPointer: {
            type: 'none'
          }
        },
        legend: {
          data: ['头图', '次图一', '次图二'],
          bottom: 20
        },
        calendar: {
          top: 'middle',
          left: 'center',
          orient: 'vertical',
          cellSize: this.cellSize,
          yearLabel: {
            show: false,
            textStyle: {
              fontSize: 30
            }
          },
          dayLabel: {
            margin: 20,
            firstDay: 1,
            nameMap: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
          },
          monthLabel: {
            show: false
          },

          silent: false,
          splitLine: false,
          range: this.range
        },
        series: [{
          id: 'label1',
          type: 'scatter',
          coordinateSystem: 'calendar',
          symbolSize: 1,
          label: {
            normal: {
              show: true,
              formatter: function(params) {
                return echarts.format.formatTime('dd', params.value[0])
              },
              offset: [-this.cellSize[0] / 2 + 10, -this.cellSize[1] / 2 + 10],
              textStyle: {
                color: '#000',
                fontSize: 14
              }
            }
          },
          // data: this.datas
          data: 123
        }]
        // series: this.getPieSeriesUpdate(this.datas, this.chart)
      }
    }
  },
  watch: {
    options(val1, val2) {
      if (this.chart) {
        console.log(this.chart, this.datas, 'chart存在')
        this.chart.setOption(this.options)
        this.chart.setOption({ series: this.getPieSeriesUpdate(this.datas, this.chart) })
      }
    }
  },
  mounted() {
    this.initChart(this.options)
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
        // console.log('resize');
        this.chart.setOption({
          series: this.getPieSeriesUpdate(this.datas, this.chart)
        })
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)

    this.chart.on('click', this.console1)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(options) {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption(options)
      this.chart.setOption({ series: this.getPieSeries(this.datas, this.chart) })
    },
    console1(val) {
      console.log(1, val)
    },

    getPieSeries(scatterData, chart) {
      console.log(scatterData)
      return echarts.util.map(scatterData, function(item, index) {
        var center = chart.convertToPixel('calendar', item)
        console.log(item, 'canlendar')
        return {
          id: index + 'pie',
          type: 'pie',
          center: center,
          label: {
            normal: {
              formatter: '',
              position: 'inside'
            }
          },
          graphic: {
            type: 'ring'
          },
          radius: pieRadius,
          data: item[2]
        }
      })
    },
    getPieSeriesUpdate(scatterData, chart) {
      console.log(scatterData)
      return echarts.util.map(scatterData, function(item, index) {
        var center = chart.convertToPixel('calendar', item)
        console.log(item, 'canlendar2')
        return {
          id: index + 'pie',
          center: center,
          data: item[2]
        }
      })
    }
  }
}
</script>
